package com.btr.ivle.helper;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.btr.ivle.R;

public class WebcastAdapter extends ArrayAdapter<Map<String,String>>{
	
    private Context context;
    
    public WebcastAdapter(Context context, List<Map<String, String>> list) {
    	super(context, 0, list);
        this.context = context;
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
    	View view = convertView;
    	view = LayoutInflater.from(getContext()).inflate(R.layout.webcastlist, null);
    	TextView tvTitle = (TextView) view.findViewById(R.id.wcTitle);
        tvTitle.setText(getItem(position).get("WebcastFileTitle"));
        TextView tvDesc = (TextView) view.findViewById(R.id.wcDesc);
        String htmlText = Html.fromHtml(getItem(position)
        		.get("WebcastFileDescription").replace("<br>", " ")).toString();
        tvDesc.setText(htmlText);
        
        ImageView iv = (ImageView) view.findViewById(R.id.imagePlayVideo);
        iv.setTag(position);
        iv.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) 
            {
            	final int pos = (Integer) v.getTag();
            	Uri intentUri = Uri.parse(getItem(pos).get("WebcastMP4"));
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setDataAndType(intentUri, "video/mp4");
                context.startActivity(intent);
//                try {
//	                HttpClient httpclient = new DefaultHttpClient();
//	            	HttpPost httppost = new HttpPost("https://ivle.nus.edu.sg/api/Lapi.svc/Webcast_AddLog_JSON");
//	            	List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
//	            	nameValuePairs.add(new BasicNameValuePair("APIKey", context.getString(R.string.IVLE_API_KEY)));
//	                nameValuePairs.add(new BasicNameValuePair("AuthToken", IVLEToken.getToken()));
//	                nameValuePairs.add(new BasicNameValuePair("MediaChanneItemID", getItem(pos).get("WebcastID")));
//					httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));
//	                httpclient.execute(httppost);
//                } catch (Exception e) {
//					e.printStackTrace();
//				}
    	    }
    	});
        return view;
    }
}
